﻿Imports System.Data.OleDb

Public Class PlayQuiz
    Inherits System.Web.UI.Page

 


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            If (Not Page.IsPostBack) Then
                'add the visitor 
                addVisitor()

                'start counting Time
                Session("startDateTime") = DateTime.Now

                'create viewStates
                ToolkitScriptManager1.RegisterPostBackControl(btnProceed)


                ViewState("picture1Selected") = ""
                ViewState("picture2Selected") = ""
                ViewState("pictureMatched") = False
                lblMatchedPictures.Text = "6"

                imgProceed.Visible = False
                btnProceed.Visible = False
                lblTotalTime.Visible = False


                'bring all the quiz_questions pictures with quizId
                Dim myQuizImages As ArrayList = New ArrayList()

                Dim selectedQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
                Dim dr As OleDbDataReader = selectedQuestion.SelectQuiz_QuestionByQuizId(Convert.ToInt32(Request.QueryString("quizId")))

                If dr.HasRows Then
                    Do While dr.Read()
                        If Not IsDBNull(dr("Quiz_QuestionPictureUrl")) Then myQuizImages.Add(dr("Quiz_QuestionPictureUrl"))
                    Loop
                End If


                For index = 0 To myQuizImages.Count - 1
                    Dim currentImageUrl As String = myQuizImages(index)
                    Dim positionFoundPicture1 As Boolean = False
                    Dim positionFoundPicture2 As Boolean = False

                    Do
                        Randomize()
                        Dim x_value As Integer = CInt(Int((4 * Rnd()) + 1))
                        Dim y_value As Integer = CInt(Int((3 * Rnd()) + 1))
                        Dim imgNamePosition As String = "img" + x_value.ToString() + "_" + y_value.ToString()

                        Dim cph As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)

                        If (DirectCast(cph.FindControl(imgNamePosition), ImageButton).CommandName = "") Then
                            DirectCast(cph.FindControl(imgNamePosition), ImageButton).CommandName = currentImageUrl
                            positionFoundPicture1 = True
                        End If

                    Loop While positionFoundPicture1 = False




                    Do
                        Randomize()
                        Dim x_value As Integer = CInt(Int((4 * Rnd()) + 1))
                        Dim y_value As Integer = CInt(Int((3 * Rnd()) + 1))
                        Dim imgNamePosition As String = "img" + x_value.ToString() + "_" + y_value.ToString()

                        Dim cph As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)

                        If (DirectCast(cph.FindControl(imgNamePosition), ImageButton).CommandName = "") Then
                            DirectCast(cph.FindControl(imgNamePosition), ImageButton).CommandName = currentImageUrl
                            positionFoundPicture2 = True
                        End If

                    Loop While positionFoundPicture2 = False


                Next

            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub addVisitor()
        Try

            Dim aCookie As HttpCookie = Request.Cookies("userIdCookie")
            Dim userId = Convert.ToInt32(aCookie.Value)

            Dim pageName As String = Request.Url.PathAndQuery
            pageName = pageName.Replace("/", "")

            Dim getPageNameId As PagesNamesManagement = New PagesNamesManagement()
            Dim dr As OleDbDataReader = getPageNameId.SelectPagesNamesByPageNameContent(pageName)

            Dim pageId As Int32 = -1

            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("PageNameId")) Then pageId = Convert.ToInt32(dr("PageNameId"))
            End If


            If (pageId <> -1) Then

                Dim insertUser As User_PagesNameManagement = New User_PagesNameManagement()
                insertUser.InsertUserPageName(userId, pageId)

            End If


        Catch ex As Exception

        End Try
    End Sub

    Private Sub checkImages(ByVal pictureSelected As String)
        Try



            If (ViewState("picture1Selected") = "") Then

                ViewState("picture1Selected") = pictureSelected
                Dim cph As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
                DirectCast(cph.FindControl(pictureSelected), ImageButton).ImageUrl = DirectCast(cph.FindControl(pictureSelected), ImageButton).CommandName()

            ElseIf ViewState("picture2Selected") = "" Then

                ViewState("picture2Selected") = pictureSelected
                Dim cph As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
                DirectCast(cph.FindControl(pictureSelected), ImageButton).ImageUrl = DirectCast(cph.FindControl(pictureSelected), ImageButton).CommandName()

                Dim cph2 As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)


                If (DirectCast(cph2.FindControl(ViewState("picture1Selected")), ImageButton).CommandName() = DirectCast(cph.FindControl(pictureSelected), ImageButton).CommandName()) Then
                    If ((DirectCast(cph2.FindControl(ViewState("picture1Selected")), ImageButton).ID) <> (DirectCast(cph.FindControl(pictureSelected), ImageButton).ID)) Then

                        ViewState("pictureMatched") = True
                        DirectCast(cph2.FindControl(ViewState("picture1Selected")), ImageButton).Enabled = False
                        DirectCast(cph.FindControl(pictureSelected), ImageButton).Enabled = False

                        lblMatchedPictures.Text = (Convert.ToInt32(lblMatchedPictures.Text) - 1).ToString()
                        If (Convert.ToInt32(lblMatchedPictures.Text) = 0) Then
                            imgProceed.Visible = True
                            btnProceed.Visible = True
                            lblTotalTime.Visible = True

                            Dim startTime As DateTime = Convert.ToDateTime(Session("startDateTime"))
                            Dim endTime As DateTime = DateTime.Now

                            Dim timeDifference As TimeSpan = endTime - startTime
                            lblTotalTime.Text = "It took you " + timeDifference.TotalSeconds.ToString().Substring(0, 2) + " seconds to finish"


                            'insert the time the user took to guess the game
                            Dim insertQuizGameTime As QuizGameTimeManagement = New QuizGameTimeManagement()
                            Dim userId As Int32 = New aspnet_UsersManagement().SelectUserIDByUsername(User.Identity.Name)
                            insertQuizGameTime.InsertQuizGameTime((Convert.ToInt32(Request.QueryString("quizId"))), userId, timeDifference.TotalSeconds)

                        End If

                    End If


                End If

            ElseIf (ViewState("picture2Selected") <> "") Then
                If (ViewState("pictureMatched") = True) Then

                    ViewState("picture1Selected") = pictureSelected
                    Dim cph As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
                    DirectCast(cph.FindControl(pictureSelected), ImageButton).ImageUrl = DirectCast(cph.FindControl(pictureSelected), ImageButton).CommandName()


                    ViewState("picture2Selected") = ""

                    ViewState("pictureMatched") = False

                Else
                    Dim cph3 As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
                    DirectCast(cph3.FindControl(ViewState("picture1Selected")), ImageButton).ImageUrl = "Pictures/Quiz/questionMark.jpg"

                    ViewState("picture1Selected") = pictureSelected

                    Dim cph As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
                    DirectCast(cph.FindControl(pictureSelected), ImageButton).ImageUrl = DirectCast(cph.FindControl(pictureSelected), ImageButton).CommandName()

                    Dim cph2 As ContentPlaceHolder = DirectCast(Me.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
                    DirectCast(cph2.FindControl(ViewState("picture2Selected")), ImageButton).ImageUrl = "Pictures/Quiz/questionMark.jpg"
                    ViewState("picture2Selected") = ""
                End If


            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub img1_1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img1_1.Click
        Try


            checkImages(img1_1.ID)

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img1_2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img1_2.Click
        Try


            checkImages(img1_2.ID)

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img1_3_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img1_3.Click
        Try


            checkImages(img1_3.ID)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img2_1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img2_1.Click
        Try


            checkImages(img2_1.ID)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img2_2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img2_2.Click
        Try


            checkImages(img2_2.ID)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img2_3_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img2_3.Click
        Try

            checkImages(img2_3.ID)

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img3_1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img3_1.Click
        Try


            checkImages(img3_1.ID)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img3_2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img3_2.Click
        Try

            checkImages(img3_2.ID)

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img3_3_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img3_3.Click
        Try

            checkImages(img3_3.ID)

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img4_1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img4_1.Click
        Try

            checkImages(img4_1.ID)


        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img4_2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img4_2.Click
        Try

        
            checkImages(img4_2.ID)
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub img4_3_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles img4_3.Click
        Try

            checkImages(img4_3.ID)

        Catch ex As Exception

        End Try
    End Sub

    Protected Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgProceed.Click
        Try

            Session("QuizId") = Request.QueryString("quizId")
            Response.Redirect("PlayQuizQuestions.aspx")

        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnProceed_Click(sender As Object, e As EventArgs) Handles btnProceed.Click
        Try

            Session("QuizId") = Request.QueryString("quizId")
            Response.Redirect("PlayQuizQuestions.aspx")


        Catch ex As Exception

        End Try

    End Sub
End Class